const puppeteer = require("puppeteer");
const devices = require("puppeteer/DeviceDescriptors");
const iPhone = devices["iPhone 11"];
let timeout = function(delay) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      try {
        resolve(1);
      } catch (e) {
        reject(0);
      }
    }, delay);
  });
};

(async () => {
  const browser = await puppeteer.launch({
    headless: false //这里我设置成false主要是为了让大家看到效果，设置为true就不会打开浏览器
  });

  const page = await browser.newPage();

  await page.emulate({
    name: "Galaxy S5", //设备名
    userAgent:
      "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Mobile Safari/537.36", //UA
    viewport: {
      width: 375, //屏幕宽度
      height: 1334, //屏幕高度
      deviceScaleFactor: 3, //缩放比例
      isMobile: true, //是否是移动设备
      hasTouch: true, //是否支持touch事件
      isLandscape: false //是否横屏
    }
  });

  console.log("进入页面");
  await page.goto("http://zhibo.sina.cn/sports_live/2019060809");
  await timeout(1000);
  await page.screenshot({
    path: "LIVE.png"
  });

  await page.mouse.move(0, 0);
  await page.mouse.down();
  await page.mouse.move(0, 100);
  await page.mouse.move(100, 100);
  await page.mouse.move(100, 0);
  await page.mouse.move(0, 0);
  await page.mouse.up();
  console.log("进入页面");

  // console.log("登录");
  // await page.tap("#u"); //直接操作dom选择器，是不是很方便
  // await page.keyboard.type("876910486",{delay: 100});

  // await page.tap("#p");
  // await page.keyboard.type("zxf19950613.",{delay: 100}) //这里密码就不展示了哈

  // await page.tap("#go");

  // await page.screenshot({
  //   path: "3.png"
  // });
  // console.log("screnshot successful");
  // await browser.close();
})();

// const puppeteer = require('puppeteer');
// const devices = require('puppeteer/DeviceDescriptors');
// const iPhone = devices['iPhone 6'];

// (async () => {

//    try{
//     const browser = await puppeteer.launch();

//     const page = await browser.newPage();

//     await page.emulate(iPhone);

//     await page.tracing.start({path: './trace.json'});
//     await page.goto('https://y.qq.com/m/digitalbum/gold/index.html?_video=true&id=2210323&g_f=tuijiannewupload#index/fans');
//     await page.tracing.stop();

//     browser.close();
//    } catch (e) {
//         console.log(e.message);
//     }
// })();
